package simportal.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cfuture08.eweb4j.component.dwz.DWZUtil;
import com.cfuture08.eweb4j.component.dwz.MainModelView;
import com.cfuture08.eweb4j.component.dwz.vo.NavMenu;
import com.cfuture08.eweb4j.mvc.annotation.Controller;
import com.cfuture08.eweb4j.mvc.annotation.Param;
import com.cfuture08.eweb4j.mvc.annotation.RequestMapping;
import com.cfuture08.eweb4j.mvc.annotation.Singleton;
import com.cfuture08.eweb4j.orm.dao.factory.DAOFactory;

/**
 * 默认控制器，负责后台主页的显示，统计等操作。
 * @author weiwei
 *
 */
@Controller
@Singleton
@RequestMapping("admin")
public class BackControl {
	/**
	 * 显示后台界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/main")
	public String admin(HttpServletRequest request){
		String treeMenus = DWZUtil.getAccordion(1);
		if (treeMenus == null){
			treeMenus = "";
		}
		
		List<NavMenu> navMenus = DAOFactory.getSelectDAO().selectAll(NavMenu.class, "rank", 1);
		MainModelView mmv  = new MainModelView();
		mmv.setTitle("");
		mmv.setNavMenus(navMenus);
		mmv.setTreeMenus(treeMenus);
		
		request.setAttribute("MainModel", mmv);
		return "main.jsp";
	}
	
	/**
	 * 切换导航菜单
	 * @param navMenuId
	 * @param out
	 * @return
	 */
	@RequestMapping("/switchEnv/{id}")
	public String switchEnvHandler(@Param("id")Integer navMenuId,PrintWriter out){
		String menu = DWZUtil.getAccordion(navMenuId);
		if (menu == null){
			menu = "";
		}
		String format = "<div class=\"accordion\" fillSpace=\"sideBar\">%s</div>";
		out.print(String.format(format,menu));
		return null;
	}
}
